home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / src / mosmllib / Char.sml < prev    next >
Encoding:
Text File  |  1996-07-03  |  2.6 KB  |  84 lines  |  [TEXT/R*ch]

  1. (* Char -- new basis 1995-05-01 *)
  2.  
  3. type char = char
  4. (* Invariant: for c: char it holds that 0 <= ord c <= maxOrd *)
  5.  
  6. exception Chr = Chr
  7.  
  8. local 
  9.     prim_val sub_    : string -> int -> char = 2 "get_nth_char"
  10.     prim_val chr_    : int -> char           = 1 "identity";
  11.     prim_val length_ : string -> int         = 1 "string_length";
  12. in
  13.     prim_val ord : char -> int               = 1 "identity";
  14.     val minChar = #"\000"
  15.     val maxChar = #"\255"
  16.     val maxOrd = 255;
  17.  
  18.     fun chr i = if i<0 orelse i>maxOrd then raise Chr else chr_ i;
  19.  
  20.     fun succ c = 
  21.     if c < maxChar then chr_(ord c + 1) else raise Chr;
  22.     fun pred c = 
  23.     if c > minChar then chr_(ord c - 1) else raise Chr;
  24.  
  25.     local
  26.     prim_type array_
  27.     prim_val array_    : int -> array_ = 1 "create_string";
  28.     prim_val update_   : array_ -> char -> int -> unit 
  29.                    = 3 "set_nth_char";
  30.     prim_val fill_     : array_ -> int -> int -> int -> unit 
  31.                   = 4 "fill_string";
  32.     prim_val subarray_ : array_ -> char -> int = 2 "get_nth_char"
  33.  
  34.         fun mktable s =
  35.         let val len = length_ s
  36.         val table = array_ 256
  37.         fun init i = 
  38.             if i < len then (update_ table (sub_ s i) 1; init (i+1))
  39.             else ()            
  40.         in
  41.         fill_ table 0 256 0;
  42.         init 0;
  43.         table
  44.         end
  45.     in
  46.     fun contains s = 
  47.         let val table = mktable s 
  48.         in fn c => subarray_ table c = 1 end
  49.  
  50.     fun notContains s = 
  51.         let val table = mktable s 
  52.         in fn c => subarray_ table c = 0 end
  53.     end
  54.  
  55.     fun isLower c  = #"a" <= c andalso c <= #"z"
  56.     fun isUpper c  = #"A" <= c andalso c <= #"Z"
  57.     fun isDigit c  = #"0" <= c andalso c <= #"9"
  58.     fun isAlpha c  = isLower c orelse isUpper c
  59.     fun isHexDigit c = isDigit c orelse #"a" <= c andalso c <= #"f"
  60.                    orelse #"A" <= c andalso c <= #"F"
  61.     fun isAlphaNum c = isAlpha c orelse isDigit c
  62.     fun isPrint c  = c >= #" " andalso c <> #"\127" andalso c <> #"\255" 
  63.     fun isSpace c  = c = #" " orelse #"\009" <= c andalso c <= #"\013"
  64.     fun isGraph c  = isPrint c andalso not (isSpace c)
  65.     fun isPunct c  = isGraph c andalso not (isAlphaNum c)
  66.     fun isAscii c  = c <= #"\127"
  67.     fun isCntrl c  = c < #" " orelse c = #"\127" orelse c = #"\255"
  68.  
  69.     fun toLower c = 
  70.     if #"A" <= c andalso c <= #"Z" then chr_(ord c + 32)
  71.     else c;
  72.     fun toUpper c = 
  73.     if #"a" <= c andalso c <= #"z" then chr_(ord c - 32)
  74.     else c;
  75.  
  76.     fun compare (x, y: char) = 
  77.     if x<y then LESS else if x>y then GREATER else EQUAL;
  78.  
  79.     val op <  = op < : char * char -> bool;
  80.     val op <= = op <= : char * char -> bool;
  81.     val op >  = op >  : char * char -> bool;
  82.     val op >= = op >= : char * char -> bool;
  83. end
  84.